var controller = {
	/**
	 * 初始化
	 */
	init : function() {
		var _this = this;
		_this.bindClick();
	},
	/**
	 * 绑定事件
	 */
	bindEvent : function() {
		var _this = this;
		_this.bindClick()
	},
	bindClick : function() {

		$("#addDialogBtn").bind("click", function() {
			$("#editWarningReceiverBtn").addClass("hide");
			$("#addWarningReceiverBtn").removeClass("hide");
			var industry_concepts = ajax.load("/conceptcode?classification=4", "查询标签失败");
			App.seSelectConcept("industry_classification", industry_concepts);
			$("#addDialog").modal('show');
		});

		$("#addWarningReceiverBtn").bind(
				"click",
				function() {
					
					$("#addDialog").modal('hide');
					
					// 资源属性
					var receiver_name = $("#receiver_name").val();
					var company = $("#company").val();
					var industry_classification = 4;
					var industry_concept = $('#industry_classification option:selected').val();
					if (industry_concept == "unselect") {
						App.myDialog("请选择行业", "提示");
						return; 
					}
					var position = $("#position").val();
					var phone_number = $("#phone_number").val();
					var email = $("#email").val();

					var warningreceiver = {
						receiver_name : receiver_name,
						company : company,
						industry_classification : industry_classification,
						industry_concept : industry_concept,
						position : position,
						phone_number : phone_number,
						email : email
					}
					var warningreceiverString = JSON.stringify(warningreceiver);
					ajax.addOrUpdate("/warningreceiver/add", warningreceiverString,
							"添加预警人员信息成功！", "添加预警人员信息失败：");
				});

		$(".remove_receiver").bind(
				"click",
				function() {
					// 资源属性
					var warningReceiverId = $(this).val();
					ajax.delete("/warningreceiver/delete?warningReceiverId="
							+ warningReceiverId, "删除预警人员信息成功！", "删除预警人员信息失败：");
				});

		$(".edit_receiver").bind(
				"click",
				function() {
					var industry_concepts = ajax.load("/conceptcode?classification=4", "查询标签失败");
					App.seSelectConcept("industry_classification", industry_concepts);
					// 资源属性
					var warningReceiverId = $(this).val();
					$("#editWarningReceiverBtn").removeClass("hide");
					$("#addWarningReceiverBtn").addClass("hide");
					var warningreceiver = ajax.load("/warningreceiver?warningReceiverId="
							+ warningReceiverId);
					$("#receiver_id").val(warningReceiverId);
					$("#receiver_name").val(warningreceiver.receiver_name);
					$("#company").val(warningreceiver.company);
					$("#industry_classification").val(warningreceiver.industry_concept);
					$("#position").val(warningreceiver.position);
					$("#phone_number").val(warningreceiver.phone_number);
					$("#email").val(warningreceiver.email);
					// ###################
					$("#addDialog").modal('show');
				});

		$("#editWarningReceiverBtn").bind(
				"click",
				function() {
					$("#addDialog").modal('hide');
					// 资源属性
					var receiver_id = $("#receiver_id").val();
					var receiver_name = $("#receiver_name").val();
					var company = $("#company").val();
					var industry_classification = 4;
					var industry_concept = $('#industry_classification option:selected').val();
					if (industry_concept == "unselect") {
						App.myDialog("请选择行业", "提示");
						return; 
					}
					var position = $("#position").val();
					var phone_number = $("#phone_number").val();
					var email = $("#email").val();

					var warningreceiver = {
						receiver_id:receiver_id,
						receiver_name : receiver_name,
						company : company,
						industry_classification : industry_classification,
						industry_concept : industry_concept,
						position : position,
						phone_number : phone_number,
						email : email
					}
					var warningreceiverString = JSON.stringify(warningreceiver);
					ajax.addOrUpdate("/warningreceiver/edit", warningreceiverString,
							"修改预警人员信息成功！", "修改预警人员信息失败：");
				});
		
		$("#importBtn").bind("click", function() {
			$("#importDialog").modal('show');
		});
		
		$("#importSubmit").bind("click", function() {
			// stop submit the form, we will post it manually.
	        event.preventDefault();
	        $("#importDialog").modal('hide');
	        $("#progressBar").modal('show');
	        fire_ajax_submit();
		});
		
		function fire_ajax_submit() {
		    // Get form
		    var form = $('#fileUploadForm')[0];
		    debugger
		    var data = new FormData(form);
		    
		    if(data.getAll("file").length == 1) {
		    	// 是否需要对文件格式做校验 每次导入都是新增
		    	var fileName = data.getAll("file")[0].name;
		    	data.append("CustomField", "This is some extra data, testing");
		    	$.ajax({
		    		type: "POST",
		    		enctype: 'multipart/form-data',
		    		url: "/warningreceiver/upload",
		    		data: data,
		    		processData: false, 
		    		contentType: false,
		    		cache: false,
		    		timeout: 600000,
		    		success: function (data) {
		    			$("#progressBar").modal('hide');
		    			console.log("SUCCESS : ", data);
		    			App.myDialog("导入成功", "提示");
		    		},
		    		error: function (e) {
		    			$("#progressBar").modal('hide');
		    			console.log("ERROR : ", e);
		    			App.myDialog("导入出错，请联系管理员", "提示");
		    		}
		    	});
		    }else {
		    	App.myDialog("请选择一个文件", "提示");
		    }
		}
	}
};
$(function() {
	controller.init();
});